<?php
/**
 * Exstension for MyArcadePlugin
 *
 * This file extends WPSocialStreamer to support WordPress actions
 *
 * @package WPSocialStreamer
 * @subpackage ExstensionWordPress
 */
 
// No direct access!
if ( !defined('ABSPATH') ) exit;

// Register needed hooks
add_action( 'publish_post', 'wpsocialstreamer_wordpress_publish_post');  
add_action( 'comment_post', 'wpsocialstreamer_wordpress_comment_post', 10, 2);
// Add extension options filter 
add_filter( 'wpsocialstreamer_extensions', 'wpsocialstreamer_extension_wordpress_settings' );

/**
 * Extend the extension array
 * 
 * @param type $extensions_array
 * @return type
 */
function wpsocialstreamer_extension_wordpress_settings( $extensions_array ) {
  
  $extensions_array['wordpress'] = array (
    /* Set the title that will be displayed on the settings page */
    'title'     => __('WordPress', 'wpsocialstreamer'),
    /* Set a function that should be used to checked if the plugin is installed */
    'function'	=> false,
    /* Set the link to the plugin */
    'link'		=> 'http://wordpress.org',
    /* Extension status */
    'active' => true,
    /* Define an array with available events / actions */
    /* ATTENTION: events should be named exactly like the corresponding action! */      
    'events' => array (
      'publish_post'  => array (
        /* Set the default message template */
        'template'  => __("Hey, I just added %POST_NAME%!", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I publish a new post.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
          'POST_NAME' => __("Name of the post", 'wpsocialstreamer')
        ) /* END placeholders */
      ), /* END event */
      'comment_post'  => array (
        /* Set the default message template */
        'template'  => __("Wohoo, I just commented on %POST_LINK%!", 'wpsocialstreamer'),
        /* Set the event description for user settings page. */
        /* Share this event when: */
        'title'     => __("I post a new comment.", 'wpsocialstreamer'),
        /* Define some placeholders that admins can use to generate message text */
        /* Set to false if no placeholders are available: 'placeholders' => false */
        'placeholders' => array ( 
          /* Set placeholder name and a short description */
          'POST_NAME' => __("Name of the post", 'wpsocialstreamer'),
          'CONTENT'   => __("Post content", 'wpsocialstreamer')
        ) /* END placeholders */
      ) /* END event */
    )
  );
    
  return $extensions_array;
}
 
/**
 * Called when user publishes a post
 */
function wpsocialstreamer_wordpress_publish_post( $postID ) {
   global $wpss;
               
   if ( empty($postID) ) return;
   
  // Check if extension event is active
  if ( $wpss->is_active('wordpress', 'publish_post') ) {
  
    $wpss->set_raw_message( 'wordpress', 'publish_post' );
  
    $wpss->set_data( array(
        'POST_NAME' => get_the_title( $postID ),
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'   => get_permalink( $postID )
      )
    );

    // Generate and share the message
    $wpss->generate_and_share();
  }
}

/**
 * Called when user posts a comment
 *
 * @param int $commentID
 * @param string $approval_status: "spam" , 0 = disapproved, 1 = approved
 */
function wpsocialstreamer_wordpress_comment_post($commentID, $approval_status) {
  global $wpss;
     
  // Check if extension event is active
  if ( $wpss->is_active('wordpress', 'comment_post') ) {    
    // Check if the comment is approved
    if ( $approval_status == 1 ) {
    
      $wpss->set_raw_message( 'wordpress', 'comment_post' );
      
      // Get the comment
      $comment = get_comment( $commentID );
           
      // Set needed data
      $wpss->set_data( array(
        'POST_NAME' => get_the_title( $comment->comment_post_ID ),
        'CONTENT' => $comment->comment_content,
        /* Every extension must contain the LINK field. Values: link or false */
        'LINK'   => get_permalink( $comment->comment_post_ID )
        )
      );      

      // Generate and share the message
      $wpss->generate_and_share();
    }
  }
}
?>